package com.kolaxy.provideruser.bean;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 
 * <一句话功能简述>基类 <功能详细描述>
 * 
 * @author 姓名:zhangdonglong 工号
 * @version [版本号, 2017年6月13日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@MappedSuperclass
public abstract class Model extends BaseModel{
    
    @Column(name = "create_by")
    @CreatedBy
    private Integer createBy;
    
    @Column(name = "update_by")
    @LastModifiedBy
    private Integer updateBy;
    
    /**
     * 添加时间
     */
    @Column(name = "create_time")
    @CreatedDate
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;
    
    /**
     * 最后修改时间
     */
    @Column(name = "update_time")
    @LastModifiedDate
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;
    
    /**
     * 
     */
    @Column(name = "is_delete")
    private Integer deleted;

    public Integer getCreateBy() {
		return createBy;
	}

	public void setCreateBy(Integer createBy) {
		this.createBy = createBy;
	}

	public Integer getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(Integer updateBy) {
		this.updateBy = updateBy;
	}

	public Date getCreateTime() {
        return createTime;
    }
    
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    
    public Date getUpdateTime() {
        return updateTime;
    }
    
    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
    
    public Integer getDeleted() {
        return deleted;
    }
    
    public void setDeleted(Integer deleted) {
        this.deleted = deleted;
    }

    public void disable(){
        this.deleted = 1;
    }
    
    public void updateByOtherDomain(Object other,int updateBy) {
        BeanUtils.copyProperties(other, this, getNullPropertyNames(other));
        setUpdateTime(new Date());
        setUpdateBy(updateBy);
    }
    
    public static String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
        Set<String> emptyNames = new HashSet<String>();
        for (java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null)
                emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    /**
     * 软删除
     * @param updateBy
     */
    public void softDelete(int updateBy){
        setDeleted(1);
        setUpdateBy(updateBy);
        setUpdateTime(new Date());
    }

    public Model(Integer createBy, Date createTime) {
        this.createBy = createBy;
        this.createTime = createTime;
        this.updateTime = createTime;
        setDeleted(0);
    }

    public void init(int createBy){
        setCreateBy(createBy);
        setCreateTime(new Date());
        setDeleted(0);
    }

    public Model() {
    }
}
